#include <iostream>
#include <ctime>
#include "SDL/SDL.h"


const int WIDTH = 640;
const int HEIGHT = 480;
const int BPP = 32;
const int NUMBER_OF_STARS = 5000;
const int BLACK = 0x000000;
const int WHITE = 0xffffff;
const int SHADE = 0x010101;
const int SCALE = 0xff;
const int DISTANCE = 0xff;


SDL_Surface *screen;
SDL_Event event;

int stars[NUMBER_OF_STARS][3];

void PutPixel(SDL_Surface *surface, int x, int y, int rgb) {

	*((unsigned int*)surface->pixels + y * surface->w + x) = rgb;

}

void InitStars() {

	srand(time(0));

	for (int i = 0; i < NUMBER_OF_STARS; i++) {
		stars[i][0] = rand() % WIDTH - WIDTH/2;
		stars[i][1] = rand() % HEIGHT - HEIGHT/2;
		stars[i][2] = rand() % DISTANCE;
	}
}

void DrawStars() {

	for (int i = 0; i < NUMBER_OF_STARS; i++) {

		stars[i][2] -= 1;

		if (stars[i][2] <= 0) {
			stars[i][2] = DISTANCE;
		}

		int x = ((stars[i][0] * SCALE) / stars[i][2]) + WIDTH/2;
		int y = ((stars[i][1] * SCALE) / stars[i][2]) + HEIGHT/2;

		if (x >  0 && x < WIDTH && y > 0 && y < HEIGHT) {
			PutPixel(screen, x, y, WHITE - (SHADE * stars[i][2]));
		}
	}
}

int main(int argc, char* args[]) {

	SDL_Init(SDL_INIT_VIDEO);

	screen = SDL_SetVideoMode(WIDTH, HEIGHT, BPP, SDL_SWSURFACE);

	InitStars();

	bool quit = false;

	while (quit == false) {

		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				quit = true;
			}
		}

		SDL_FillRect(screen, NULL, BLACK);
		DrawStars();
		SDL_Flip(screen);
	}

	SDL_Quit();

	return 0;
}
